package com.generator;

import com.generator.util.DateUtils;
import com.generator.util.JDBCUtils;
import com.generator.vo.ClassVO;
import com.generator.vo.FieldVO;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

/**
 * @author zqd
 */
public class Generator {
    private static final String DEF_ENCODING = "UTF-8";
    //项目的包
    private static String basePackageName = "com.dushibao";
    //代码生成器用于哪种类型 就放到resources下哪个文件夹里:mybatis、mybatis-plus、javafx。。。
    private static String type = "";
    private static String basePackageFolder = type+"com/dushibao";
    private static String webappFolder = "webapp/WEB-INF/jsp";

    //类名
    private static String className = "Advert";

    //表名
    private static String tableName = "dushibao_advert";

    private static ClassVO classVO = new ClassVO();

    static{
        classVO.setBasePackageName(basePackageName);
        classVO.setClassName(className);
        classVO.setEntityName(classVO.getClassName().substring(0,1).toLowerCase()+classVO.getClassName().substring(1));
        classVO.setTableName(tableName);
        classVO.setDate(DateUtils.getDate(DateUtils.DATETIME_FORMAT));
        classVO.setAuthor("zqd");
        classVO.setVersion("v1.0");
        classVO.setCopyright("Copyright: Copyright (c) 2021");
        classVO.setWebSite("www.test.com");
    }

    /**
     * ☆☆☆☆☆☆☆☆☆☆
     * ☆☆☆☆☆☆☆☆☆☆
     * ☆☆☆☆☆☆☆☆☆☆
     * ☆☆☆☆☆☆☆☆☆☆
     *
     * @param args
     * @throws Exception
     */
    public static void main(String []args) throws Exception {
        /**
         * 根据表反射数据库字段
         */
        generatorVM(basePackageFolder+"/"+"entity",type+"vm/Entity",classVO.getClassName()+".java");
        generatorVM(basePackageFolder+"/"+"dao",type+"vm/Dao",classVO.getClassName()+"Dao.java");
        generatorVM(basePackageFolder+"/"+"service",type+"vm/Service",classVO.getClassName()+"Service.java");
        generatorVM(basePackageFolder+"/"+"service/impl",type+"vm/ServiceImpl",classVO.getClassName()+"ServiceImpl.java");
        generatorVM(basePackageFolder+"/"+"servlet/"+classVO.getEntityName(),type+"vm/ListServlet","ListServlet.java");
        generatorVM(basePackageFolder+"/"+"servlet/"+classVO.getEntityName(),type+"vm/AddServlet","AddServlet.java");
        generatorVM(basePackageFolder+"/"+"servlet/"+classVO.getEntityName(),type+"vm/DeleteServlet","DeleteServlet.java");

        generatorVM(webappFolder+"/"+classVO.getEntityName(),type+"vm/add","add.jsp");
        generatorVM(webappFolder+"/"+classVO.getEntityName(),type+"vm/list","list.jsp");

    }

    /**
     * @param folder 子文件夹
     * @param vm VM文件
     * @throws SQLException
     */
    private static void generatorVM(String folder,String vm,String fileName) throws Exception {
        Generator generHtmlForVelocity = new Generator();

        classVO.setPackageFolder(folder);

        generHtmlForVelocity.doRelease(vm,fileName);
    }

    /**
     * 生成页面主体
     */
    public void doRelease(String vm,String fileName){
        try {
            Properties p = new Properties();
            p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
            p.setProperty(Velocity.ENCODING_DEFAULT, DEF_ENCODING);
            p.setProperty(Velocity.INPUT_ENCODING, DEF_ENCODING);

            Velocity.init(p);

            Template template = getVelocityTemplate(ClassLoader.getSystemClassLoader().getResource(vm+".vm").getPath());
            VelocityContext context = initVelocityContext();
            BufferedWriter writer = getWriter(fileName);

            template.merge(context, writer);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化VelocityContext
     */
    public VelocityContext initVelocityContext() throws Exception{
        VelocityContext context = new VelocityContext();
        try{
            List<FieldVO> fields = JDBCUtils.getFields2(tableName);
            JDBCUtils.setClassVO(classVO);

            context.put("fields", fields);
            context.put("classVO", classVO);
        }catch(Exception e){
            e.printStackTrace();
        }
        return context;
    }

    /**
     * 获取Velocity模板
     * @param  velocityTemplateSource
     */
    public Template getVelocityTemplate(String velocityTemplateSource) throws Exception{
        Template template = new Template();
        try{
            template = Velocity.getTemplate(velocityTemplateSource);
        }catch(Exception e){
            e.printStackTrace();
        }
        return template;
    }

    /**
     * 获取Velocity写入流
     * @param  writerStreamSource
     */
    public BufferedWriter getWriter(String fileName) throws Exception{
        try{
            String packageFolder = "src/main/resources/"+classVO.getPackageFolder();
            File f = new File(packageFolder);
            f.mkdirs();

            FileOutputStream fos = new FileOutputStream(packageFolder+"/"+fileName);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, DEF_ENCODING));
            return writer;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
